/*
 * File      : usart.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2006-2015, RT-Thread Development Team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Change Logs:
 * Date           Author       Notes
 * 2011-01-13     weety       first version
 * 2013-07-21     weety       using serial component
 * 2015-05-02     ArdaFu      Port from AT91SAM9260 BSP
 */

#include <rtthread.h>
#include <rthw.h>
#include <rtdevice.h>
#include "interrupt.h"
#include <common.h>
#include "gpio.h"

#include "uart.h"

typedef struct
{
    uint32_t port;
    IRQn_Type irq;
} asm_uart_t;


/**
 * This function will handle serial
 */
void rt_asm_usart_handler( void *param)
{
}

/**
* UART device in RT-Thread
*/
static rt_err_t asm_usart_configure(struct rt_serial_device *serial,
                                    struct serial_configure *cfg)
{
    return RT_EOK;
}

static rt_err_t asm_usart_control(struct rt_serial_device *serial,
                                  int cmd, void *arg)
{
    return RT_EOK;
}

void _PutChar_f(UINT8 ch);
static int asm_usart_putc(struct rt_serial_device *serial, char c)
{
    _PutChar_f(c);
    return 1;
}

static int asm_usart_getc(struct rt_serial_device *serial)
{
    /*
    if (!(inpw(REG_UART0_FSR) & (1 << 14)))
    {
        return (inpw(REG_UART0_RBR));
    }
    */
    
    return 0;
}

static const struct rt_uart_ops asm_usart_ops =
{
    asm_usart_configure,
    asm_usart_control,
    asm_usart_putc,
    asm_usart_getc,
};


#if defined(RT_USING_UART0)
static struct rt_serial_device serial0;
asm_uart_t uart0 =
{
    UART0,
    UART0_IRQn
};
#endif



void asm_serial_config_set_default(struct rt_serial_device* serial)
{
    serial->ops = &asm_usart_ops;

    serial->config.baud_rate = BAUD_RATE_115200;
    serial->config.bit_order = BIT_ORDER_LSB;
    serial->config.data_bits = DATA_BITS_8;
    serial->config.parity    = PARITY_NONE;
    serial->config.stop_bits = STOP_BITS_1;
    serial->config.invert    = NRZ_NORMAL;
    serial->config.bufsz     = RT_SERIAL_RB_BUFSZ;
}
#define DRV_REG_OPS (RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM)
/**
 * This function will handle init uart
 */
int rt_hw_uart_init(void)
{
#if defined(RT_USING_UART0)
    //Hw_UartInit(0);
    asm_serial_config_set_default(&serial0);

    /* register uart device */
    rt_hw_serial_register(&serial0, "uart0", DRV_REG_OPS, &uart0);
    rt_hw_interrupt_install(uart0.irq, rt_asm_usart_handler,
                            (void *)&(serial0.parent), "UART0");
    rt_hw_interrupt_umask(uart0.irq);
#endif

    return 0;
}
INIT_BOARD_EXPORT(rt_hw_uart_init);
